package com.bookworld.model;

import jakarta.validation.constraints.*;
import org.hibernate.validator.constraints.URL;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

public class Author {
    private Long id;
    
    @NotBlank(message = "{author.firstName.notblank}")
    @Size(max = 50, message = "{author.firstName.size}")
    private String firstName;
    
    @NotBlank(message = "{author.lastName.notblank}")
    @Size(max = 50, message = "{author.lastName.size}")
    private String lastName;
    
    @NotBlank(message = "{author.fullName.notblank}")
    @Size(max = 100, message = "{author.fullName.size}")
    private String fullName;
    
    @Email(message = "{author.email.invalid}")
    @Size(max = 100, message = "{author.email.size}")
    private String email;
    
    @Past(message = "{author.birthDate.past}")
    private LocalDate birthDate;
    
    @Size(max = 100, message = "{author.nationality.size}")
    private String nationality;
    
    @Size(max = 1000, message = "{author.biography.size}")
    private String biography;
    
    @URL(message = "{author.website.invalid}")
    private String website;
    
    @URL(message = "{author.photoUrl.invalid}")
    private String photoUrl;
    
    @Size(max = 100, message = "{author.occupation.size}")
    private String occupation;
    
    @Size(max = 500, message = "{author.education.size}")
    private String education;
    
    @Size(max = 500, message = "{author.awards.size}")
    private String awards;
    
    private Boolean active;
    
    private String metaTitle;
    
    @Size(max = 500, message = "{author.metaDescription.size}")
    private String metaDescription;
    
    private LocalDateTime createdAt;
    private LocalDateTime updatedAt;
    
    // 关联对象
    private List<Book> books;

    public Author() {
        this.active = true;
        this.createdAt = LocalDateTime.now();
    }

    // 生成全名
    public String generateFullName() {
        return firstName + " " + lastName;
    }

    // 获取年龄
    public Integer getAge() {
        if (birthDate != null) {
            return LocalDate.now().getYear() - birthDate.getYear();
        }
        return null;
    }

    // Getters and Setters
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
        if (lastName != null) {
            this.fullName = generateFullName();
        }
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
        if (firstName != null) {
            this.fullName = generateFullName();
        }
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public LocalDate getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(LocalDate birthDate) {
        this.birthDate = birthDate;
    }

    public String getNationality() {
        return nationality;
    }

    public void setNationality(String nationality) {
        this.nationality = nationality;
    }

    public String getBiography() {
        return biography;
    }

    public void setBiography(String biography) {
        this.biography = biography;
    }

    public String getWebsite() {
        return website;
    }

    public void setWebsite(String website) {
        this.website = website;
    }

    public String getPhotoUrl() {
        return photoUrl;
    }

    public void setPhotoUrl(String photoUrl) {
        this.photoUrl = photoUrl;
    }

    public String getOccupation() {
        return occupation;
    }

    public void setOccupation(String occupation) {
        this.occupation = occupation;
    }

    public String getEducation() {
        return education;
    }

    public void setEducation(String education) {
        this.education = education;
    }

    public String getAwards() {
        return awards;
    }

    public void setAwards(String awards) {
        this.awards = awards;
    }

    public Boolean getActive() {
        return active;
    }

    public void setActive(Boolean active) {
        this.active = active;
    }

    public String getMetaTitle() {
        return metaTitle;
    }

    public void setMetaTitle(String metaTitle) {
        this.metaTitle = metaTitle;
    }

    public String getMetaDescription() {
        return metaDescription;
    }

    public void setMetaDescription(String metaDescription) {
        this.metaDescription = metaDescription;
    }

    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }

    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }

    public List<Book> getBooks() {
        return books;
    }

    public void setBooks(List<Book> books) {
        this.books = books;
    }

    @Override
    public String toString() {
        return "Author{" +
                "id=" + id +
                ", fullName='" + fullName + '\'' +
                ", email='" + email + '\'' +
                ", active=" + active +
                '}';
    }
}
